<?php
/**
 * 默认展示页面
 *
 *
 ***/


defined('IN_NWIDC') or exit('Access Invalid!');
class indexControl extends BaseHomeControl{
	public function outInfoOp(){
		header("Access-Control-Allow-Origin: *");
		file_put_contents(WEBROOT."/data/log/outInfo".date("Ymd").".txt", json_encode($_POST, JSON_UNESCAPED_UNICODE).PHP_EOL, FILE_APPEND);
	}
	public function indexOp(){


		// if(cookie('place')){
  //       	header('location:index.php?act=index&op=show_place&id=' . cookie('place'));
 	//     }
		
		if(isMobile()){
			header("location:/wap/");
			exit;
		}
		$position = array();

		Language::read('home_index_index');
		Tpl::output('index_sign','index');

		//抢购专区
		Language::read('member_groupbuy');
        $model_groupbuy = Model('groupbuy');
        $group_list = $model_groupbuy->getGroupbuyCommendedList(4);
		Tpl::output('group_list', $group_list);
		//首页广告位1
		$model_adv = Model('adv');
		$position['ap_name'] = '首页广告位1';
		$adv_position = $model_adv->getApList($position);
		$condition = array();
		$condition['ap_id'] = $adv_position[0]['ap_id'];
		$adv_list = $model_adv->getList($condition);
		Tpl::output('adv_list',$adv_list);
		
		//首页商品列表
		// $model_goods = Model('goods');
		$goods_list = Model()->table('goods_common')->where(array('index_recommend' => 1))->order('goods_commonid desc')->select();
		foreach ($goods_list as $key => $value) {
			$goods_info = Model()->table('goods')->where(array('goods_commonid'=>$value['goods_commonid']))->find();
			$goods_list[$key]['goods_id'] = $goods_info['goods_id'];
			$goods_list[$key]['goods_salenum'] = $goods_info['goods_salenum'];
		}
		//获取店铺信息
        $model_store = Model('store');
        foreach ($goods_list as $key => $value) {
            $goods_list[$key]['store_info'] = $model_store->getStoreInfoByID($value['store_id']);
        }
		Tpl::output('goods_list',$goods_list);

		//首页品牌广告位_1
		$model_adv = Model('adv');
		$position_brand = array();
		// array(array('like','a%'));
		// $position_brand['ap_name'] = array(array('like','首页品牌广告位_%'));
		$adv_brand_position = Model()->table('adv_position')->where(array('ap_name'=>array('like','首页品牌广告位%')))->select();
		foreach ($adv_brand_position as $key => $value) {
			$ap_id[] = $value['ap_id'];
			$adv_brand[$value['ap_id']] = $value;
		}
		$ap_ids = implode(',',$ap_id);
		$condition = array();
		$condition['ap_id'] = array('in',$ap_ids);
		$adv_list_1 = Model()->table('adv')->where($condition)->select();
		foreach ($ap_id as $k => $v) {
			foreach ($adv_list_1 as $k1 => $v1) {
				if($v1['ap_id'] == $v){
					$adv_list_brand[$v][] = $v1;
				}
			}
		}
		// print_r($adv_brand);exit;
		Tpl::output('adv_brand',$adv_brand);
		Tpl::output('adv_list_1',$adv_list_brand);

		//限时折扣
        $model_xianshi_goods = Model('p_xianshi_goods');
        $xianshi_item = $model_xianshi_goods->getXianshiGoodsCommendList(4);
		Tpl::output('xianshi_item', $xianshi_item);

		//板块信息
		$model_web_config = Model('web_config');
		$web_html = $model_web_config->getWebHtml('index');
		Tpl::output('web_html',$web_html);

		Model('seo')->type('index')->show();

		#category
		

		Tpl::showpage('index');
	}

	public function show_placeOp(){
		$id = $_GET['place_id'];
		$info = Model('place')->find($id);
		if(!$info){
			header('location:/index.php');
		}
		$_SESSION['place'] = $info;
		setNcCookie('place',$info['id']);
		Tpl::output('info',$info);

		Language::read('home_brand_index');
        Language::read('home_goods_class_index');
		$lang	= Language::getLangContent();
		/**
		 * 验证品牌
		 */
		$brands = Model('brand')->where(array('from_place' => $_GET['place_id']))->select();
		if($brands){
			$arr = array_map(function($item){
				return $item['brand_id'];
			},$brands);
  			$_GET['brand'] = implode(',',$arr);
		}

		$model_brand = Model('brand');
        if(strpos($_GET['brand'],',') !== false){
            $_brands = explode(',',$_GET['brand']);
            // $_GET['brand'] = $_brands[0];
        }
		// $brand_info = $model_brand->getBrandInfo(array('brand_id' => intval($_GET['brand'])));
		// if(!$brand_info){
		// 	showMessage($lang['wrong_argument'],'index.php','html','error');
		// }

		/**
		 * 获得推荐品牌
		 */
		$brand_r_list = Model('brand')->getBrandPassedList(array('brand_recommend'=>1) ,'brand_id,brand_name,brand_pic', 0, 'brand_sort asc, brand_id desc', 10);
		Tpl::output('brand_r',$brand_r_list);

        // 得到排序方式
        $order = 'is_own_shop desc,goods_id desc';
        if (!empty($_GET['key'])) {
            $order_tmp = trim($_GET['key']);
            $sequence = $_GET['order'] == 1 ? 'asc' : 'desc';
            switch ($order_tmp) {
                case '1' : // 销量
                    $order = 'goods_salenum' . ' ' . $sequence;
                    break;
                case '2' : // 浏览量
                    $order = 'goods_click' . ' ' . $sequence;
                    break;
                case '3' : // 价格
                    $order = 'goods_promotion_price' . ' ' . $sequence;
                    break;
            }
        }

        // 字段
        $fieldstr = "goods_id,goods_commonid,goods_name,goods_jingle,store_id,store_name,goods_price,goods_promotion_price,goods_promotion_type,goods_marketprice,goods_storage,goods_image,goods_freight,goods_salenum,color_id,evaluation_good_star,evaluation_count,is_virtual,is_fcode,is_appoint,is_presell,have_gift";
        // 条件

        $where = array();
        // if(strpos($_GET['brand'],',')){

            // $where['brand_id'] = array('in',$_GET['brand']);
            $where['from_place'] = $_GET['place_id'];
        // }
        // else{
        //     $where['brand_id'] = $_GET['brand'];
        // }

        if (intval($_GET['area_id']) > 0) {
            $where['areaid_1'] = intval($_GET['area_id']);
        }
        if ($_GET['type'] == 1) {
            $where['is_own_shop'] = 1;
        }
        if ($_GET['gift'] == 1) {
            $where['have_gift'] = 1;
        }

        $_goods = Model()->table('goods_common')->field('goods_commonid')->where($where)->page(21)->select();
        $arr = array_map(function($item){
        	return $item['goods_commonid'];
        },$_goods);
        $where = array("goods_commonid" => array("in",implode(',',$arr)))	;
        $model_goods = Model('goods');
        $goods_list = $model_goods->getGoodsListByColorDistinct($where, $fieldstr, $order, 21);
        Tpl::output('show_page1', $model_goods->showpage(4));
        Tpl::output('show_page', $model_goods->showpage(5));
        
        // 商品多图
        if (!empty($goods_list)) {
            $commonid_array = array(); // 商品公共id数组
                $storeid_array = array();       // 店铺id数组
            foreach ($goods_list as $value) {
                $commonid_array[] = $value['goods_commonid'];
                $storeid_array[] = $value['store_id'];
            }
            $commonid_array = array_unique($commonid_array);
            $storeid_array = array_unique($storeid_array);
            // 商品多图
            $goodsimage_more = $model_goods->getGoodsImageList(array('goods_commonid' => array('in', $commonid_array)));
            // 店铺
            $store_list = Model('store')->getStoreMemberIDList($storeid_array);

            foreach ($goods_list as $key => $value) {
                // 商品多图
                foreach ($goodsimage_more as $v) {
                    if ($value['goods_commonid'] == $v['goods_commonid'] && $value['store_id'] == $v['store_id'] && $value['color_id'] == $v['color_id']) {
                        $goods_list[$key]['image'][] = $v;
                    }
                }
                // 店铺的开店会员编号
                $store_id = $value['store_id'];
                $goods_list[$key]['member_id'] = $store_list[$store_id]['member_id'];
                $goods_list[$key]['store_domain'] = $store_list[$store_id]['store_domain'];
                //将关键字置红
                $goods_list[$key]['goods_name_highlight'] = $value['goods_name'];
            }
        }
        Tpl::output('goods_list', $goods_list);

        // 地区
        $province_array = Model('area')->getTopLevelAreas();
        Tpl::output('province_array', $province_array);

        loadfunc('search');
        /**
         * 取浏览过产品的cookie(最大四组)
         */
        $viewed_goods = Model('goods_browse')->getViewedGoodsList($_SESSION['member_id'],20);
        Tpl::output('viewed_goods',$viewed_goods);

		/**
		 * 分类导航
		 */
		$nav_link = array(
			0=>array(
				'title'=>$lang['homepage'],
				'link'=>SHOP_SITE_URL
			),
			1=>array(
				'title'=>$lang['brand_index_all_brand'],
				'link'=>urlShop('brand', 'index')
			),
			2=>array(
				'title'=>$brand_info['brand_name']
			)
		);
		Tpl::output('nav_link_list',$nav_link);
		/**
		 * 页面输出
		 */
		Tpl::output('index_sign','brand');


		Model('seo')->type('brand_list')->param(array('name'=>$brand_info['brand_name']))->show();
		
		Tpl::showpage('guan_index');


	}

	//json输出商品分类
	public function josn_classOp() {
		/**
		 * 实例化商品分类模型
		 */
		$model_class		= Model('goods_class');
		$goods_class		= $model_class->getGoodsClassListByParentId(intval($_GET['gc_id']));
		$array				= array();
		if(is_array($goods_class) and count($goods_class)>0) {
			foreach ($goods_class as $val) {
				$array[$val['gc_id']] = array('gc_id'=>$val['gc_id'],'gc_name'=>htmlspecialchars($val['gc_name']),'gc_parent_id'=>$val['gc_parent_id'],'commis_rate'=>$val['commis_rate'],'gc_sort'=>$val['gc_sort']);
			}
		}
		/**
		 * 转码
		 */
		if (strtoupper(CHARSET) == 'GBK'){
			$array = Language::getUTF8(array_values($array));//网站GBK使用编码时,转换为UTF-8,防止json输出汉字问题
		} else {
			$array = array_values($array);
		}
		echo $_GET['callback'].'('.json_encode($array).')';
	}

    /**
     * json输出地址数组 原data/resource/js/area_array.js
     */
    public function json_areaOp()
    {
        echo $_GET['callback'].'('.json_encode(Model('area')->getAreaArrayForJson()).')';
    }

	//判断是否登录
	public function loginOp(){
		echo ($_SESSION['is_login'] == '1')? '1':'0';
	}

	/**
	 * 头部最近浏览的商品
	 */
	public function viewed_infoOp(){
	    $info = array();
		if ($_SESSION['is_login'] == '1') {
		    $member_id = $_SESSION['member_id'];
		    $info['m_id'] = $member_id;
		    if (C('voucher_allow') == 1) {
		        $time_to = time();//当前日期
    		    $info['voucher'] = Model()->table('voucher')->where(array('voucher_owner_id'=> $member_id,'voucher_state'=> 1,
    		    'voucher_start_date'=> array('elt',$time_to),'voucher_end_date'=> array('egt',$time_to)))->count();
		    }
    		$time_to = strtotime(date('Y-m-d'));//当前日期
    		$time_from = date('Y-m-d',($time_to-60*60*24*7));//7天前
		    $info['consult'] = Model()->table('consult')->where(array('member_id'=> $member_id,
		    'consult_reply_time'=> array(array('gt',strtotime($time_from)),array('lt',$time_to+60*60*24),'and')))->count();
		}
		$goods_list = Model('goods_browse')->getViewedGoodsList($_SESSION['member_id'],5);
		if(is_array($goods_list) && !empty($goods_list)) {
		    $viewed_goods = array();
		    foreach ($goods_list as $key => $val) {
		        $goods_id = $val['goods_id'];
		        $val['url'] = urlShop('goods', 'index', array('goods_id' => $goods_id));
		        $val['goods_image'] = thumb($val, 60);
		        $viewed_goods[$goods_id] = $val;
		    }
		    $info['viewed_goods'] = $viewed_goods;
		}
		if (strtoupper(CHARSET) == 'GBK'){
			$info = Language::getUTF8($info);
		}
		echo json_encode($info);
	}
	/**
	 * 查询每月的周数组
	 */
	public function getweekofmonthOp(){
	    import('function.datehelper');
	    $year = $_GET['y'];
	    $month = $_GET['m'];
	    $week_arr = getMonthWeekArr($year, $month);
	    echo json_encode($week_arr);
	    die;
	}
}
